<?php

class index {
	private $token = "weixin";

	public $timestamp;
	public $nonce; 
	public $signature; 	
	public $echostr; 	
	
	public function __construct(){
		var_dump($_GET);exit;
		$this->timestamp = $_GET["timestamp"];
		$this->nonce = $_GET["nonce"];
		$this->signature = $_GET["signature"];
		$this->echostr = $_GET["echostr"];
		
		$arr = array($this->token , $this->timestamp , $this->nonce);
		sort($arr , SORT_STRING);
		$str = implode($arr);
		$str = sha1($str);
		if($str == $this->signature && $this->echostr){
			echo $this->echostr;
			exit;
		}else{
			$this->responseText();
		}		
	}	
	
	
	
	
	function responseText(){
		$httpPost = $GLOBALS["HTTP_RAW_POST_DATA"];
		$obj = simplexml_load_string($httpPost);
		/*
		<xml>
			<ToUserName><![CDATA[toUser]]></ToUserName>
			<FromUserName><![CDATA[FromUser]]></FromUserName>
			<CreateTime>123456789</CreateTime>
			<MsgType><![CDATA[event]]></MsgType>
			<Event><![CDATA[subscribe]]></Event>
		</xml>
		*/
		$to = $obj->ToUserName;
		$from = $obj->FromUserName;
		$type = $obj->MsgType;
		$event = $obj->Event;
		if(strtolower($type) == "event"){
			if(strtolower($event) == "subscribe"){
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[text]]></MsgType>
				<Content><![CDATA[你好！这是服务器端！]]></Content>
				</xml>";
				echo $text;
			}
			
			if(strtolower($event) == "click"){
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[event]]></MsgType>
				<Event><![CDATA[CLICK]]></Event>
				<EventKey><![CDATA[DY]]></EventKey>
				</xml>";
				echo $text;
			}
		}
		
		
		if(strtolower($type) == "text"){
			if(trim($obj->Content) == "小刚"){
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[text]]></MsgType>
				<Content><![CDATA[你好！欢迎来到小刚的个人空间！]]></Content>
				</xml>";
				echo $text;
			}elseif(trim($obj->Content) == "电影"){
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[text]]></MsgType>
				<Content><![CDATA[你好！这里有最新的电影资讯！]]></Content>
				</xml>";
				echo $text;
			}elseif(trim($obj->Content) == "图片"){
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[image]]></MsgType>
				<Image>
				<MediaId><![CDATA[WscTOHZ4TAOKxzyrVHHTwHk_j-Gvj0hn4jFJqpEc0gg2IW8iBNsEUjLnRI9yOf5X]]></MediaId>
				</Image>
				</xml>";
				echo $text;
			}elseif(trim($obj->Content) == "新闻"){
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[news]]></MsgType>
				<ArticleCount>2</ArticleCount>
				<Articles>
				<item>
				<Title><![CDATA[简单快乐的幸福！马上拥有]]></Title> 
				<Description><![CDATA[马上拥有]]></Description>
				<PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/MmV1bhFZFHDvx3GTIrkahoECYfyFVAJWibPRuF19MOA7RJTct2f3giaM2ULPpabibOIy5n5ngW5tDibLRZChqvUdcw/0]]></PicUrl>
				<Url><![CDATA[http://139.129.47.213/muen/]]></Url>
				</item>
				<item>
				<Title><![CDATA[女性保健新年抗衰养生计划]]></Title>
				<Description><![CDATA[女性保健]]></Description>
				<PicUrl><![CDATA[http://139.129.47.213/muen/img/apo2.jpg]]></PicUrl>
				<Url><![CDATA[http://139.129.47.213/muen/]]></Url>
				</item>
				</Articles>
				</xml>";
				echo $text;
			}elseif(trim($obj->Content) == "语音"){
				$text = "
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[voice]]></MsgType>
				<Voice>
				<MediaId><![CDATA[1.mp3]]></MediaId>
				</Voice>
				</xml>";
				echo $text;
			}else{
				$text ="
				<xml>
				<ToUserName><![CDATA[".$from."]]></ToUserName>
				<FromUserName><![CDATA[".$to."]]></FromUserName>
				<CreateTime>".time()."</CreateTime>
				<MsgType><![CDATA[text]]></MsgType>
				<Content><![CDATA[你好！你可以回复“小刚”、“新闻”或者“电影”查看更多内容！]]></Content>
				</xml>";
				echo $text;
			}
			
			
		}
	}	
	
}	
$test = new index();


?>